package com.sanpu.kxe.business.main.presenter;

import android.text.TextUtils;

import com.sanpu.kxe.R;
import com.sanpu.kxe.business.main.bean.CertificationOldOutput;
import com.sanpu.kxe.business.main.bean.UserCenterOutput;
import com.sanpu.kxe.business.main.contract.OldCertificationContract;
import com.sanpu.kxe.business.main.model.MainModel;
import com.sanpu.kxe.common.cache.CacheManage;
import com.sanpu.kxe.common.cache.CacheModel;
import com.sanpu.kxe.common.interfaces.CommonConfig;
import com.sanpu.kxe.module.dialog.GlobalDialog;
import com.sanpu.kxe.utils.ToastUtil;
import com.sanpu.kxe.utils.VerifyUtil;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Description：实名认证Presenter
 * Created by 薛勇军 on 2019/5/15.
 */

public class OldCertificationPresenter implements OldCertificationContract.Presenter {

    private OldCertificationContract.View mView;

    private CompositeSubscription mSubscription;

    private GlobalDialog failDialog;

    public OldCertificationPresenter(OldCertificationContract.View mView) {
        this.mView = mView;
        mSubscription = new CompositeSubscription();
    }

    @Override
    public void initDataView() {
        UserCenterOutput centerOutput = CacheManage.getInstance().getCache(CacheModel.USER_CENTER_KEY);
        if (centerOutput != null) {
            String chkSts = centerOutput.getChkSts();
            if (!TextUtils.equals(CommonConfig.AUTHEN_NOTSUB, chkSts)) {
                getAuthInfo();
            }
//            else {
//                mView.showFirstDialog();
//            }
        }
    }

    /**
     * 获取认证信息
     */
    private void getAuthInfo() {
        mView.showLoadingDialog(false);
        Subscription subscription = MainModel.getInstance().getAuthInfo()
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<CertificationOldOutput>() {
                    @Override
                    public void onCompleted() {
                        mView.dismissLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoadingDialog();
                        mView.showErrorText(e);
                    }

                    @Override
                    public void onNext(CertificationOldOutput authInfoOutput) {
                        if (authInfoOutput != null &&
                                TextUtils.equals(CommonConfig.AUTHEN_NOPASS, authInfoOutput.getChkSts())) {
                            if (failDialog == null) {
                                failDialog = new GlobalDialog(mView.getContext())
                                        .content(authInfoOutput.getRmk())
                                        .headerIcon(R.drawable.img_fail_result);
                            }
                            if (!failDialog.isShowing())
                                failDialog.show();
                        }
                        mView.showViewData(authInfoOutput);
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void onCommitData(String name, String identityNumber, String bankCardNumber, String bankPhoneNumber) {
        if (TextUtils.isEmpty(name)) {
            ToastUtil.showTextViewPrompt("必须填写名字");
            return;
        }
        if (TextUtils.isEmpty(identityNumber)) {
            ToastUtil.showTextViewPrompt("必须填写身份证号");
            return;
        }
        if (VerifyUtil.checkIdValidation(identityNumber)) {
            ToastUtil.showTextViewPrompt("身份证号填写不正确");
            return;
        }
        if (TextUtils.isEmpty(bankCardNumber)) {
            ToastUtil.showTextViewPrompt("结算卡号不能为空");
            return;
        }
        if (TextUtils.isEmpty(bankPhoneNumber)) {
            ToastUtil.showTextViewPrompt("手机号不能为空");
            return;
        }
        if (VerifyUtil.checkPhoneNumber(bankPhoneNumber)) {
            ToastUtil.showTextViewPrompt("请输入正确的手机号");
            return;
        }
        mView.showLoadingDialog(false, "正在认证中...");
        Subscription subscription = MainModel.getInstance().authentication(name, identityNumber,
                bankCardNumber.replace(" ", ""), bankPhoneNumber)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onCompleted() {
                        mView.dismissLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoadingDialog();
                        mView.showErrorText(e);
                    }

                    @Override
                    public void onNext(String s) {
                        mView.openCertificateSecondActivity();
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void onDestroyPresenter() {
        if (mSubscription != null && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
        if (failDialog != null && failDialog.isShowing()) {
            failDialog.dismiss();
            failDialog = null;
        }
        if (mView != null) {
            mView = null;
        }
    }
}
